package com.jh.user.service;

import cn.jh.common.exception.ServiceException;
import com.jh.user.pojo.enums.ChannelMerType;
import com.jh.user.pojo.enums.PayType;
import com.jh.user.pojo.enums.UserGrade;

import java.math.BigDecimal;

/**
 * @author Yichuan
 * @version 1.0
 */
public class BrandRateRequest {

    private Long id;
    private Long brandId;
    private Long channelId;
    private Short grade;
    private BigDecimal rate;
    private BigDecimal minRate;
    private BigDecimal extraFee;
    private BigDecimal gradeRate;
    private Short type;

    private transient UserGrade userGrade;
    private transient PayType payType;

    private String merType;
    private transient ChannelMerType channelMerType;

    public BrandRateRequest() {
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public Long getBrandId() {
        return brandId;
    }

    public void setBrandId(Long brandId) {
        this.brandId = brandId;
    }

    public Long getChannelId() {
        return channelId;
    }

    public void setChannelId(Long channelId) {
        this.channelId = channelId;
    }

    public Short getGrade() {
        return grade;
    }

    public void setGrade(Short grade) {
        this.grade = grade;
    }

    public BigDecimal getRate() {
        return rate;
    }

    public void setRate(BigDecimal rate) {
        this.rate = rate;
    }

    public BigDecimal getMinRate() {
        return minRate;
    }

    public void setMinRate(BigDecimal minRate) {
        this.minRate = minRate;
    }

    public BigDecimal getExtraFee() {
        return extraFee;
    }

    public void setExtraFee(BigDecimal extraFee) {
        this.extraFee = extraFee;
    }

    public BigDecimal getGradeRate() {
        return gradeRate;
    }

    public void setGradeRate(BigDecimal gradeRate) {
        this.gradeRate = gradeRate;
    }

    public Short getType() {
        return type;
    }

    public void setType(Short type) {
        this.type = type;
    }

    public UserGrade getUserGrade() {
        return userGrade;
    }

    public void setUserGrade(UserGrade userGrade) {
        this.userGrade = userGrade;
    }

    public PayType getPayType() {
        return payType;
    }

    public void setPayType(PayType payType) {
        this.payType = payType;
    }

    public String getMerType() {
        return merType;
    }

    public void setMerType(String merType) {
        this.merType = merType;
    }

    public ChannelMerType getChannelMerType() {
        return channelMerType;
    }

    public void setChannelMerType(ChannelMerType channelMerType) {
        this.channelMerType = channelMerType;
    }

    public void validation() {
        if (getBrandId() == null) {
            throw new ServiceException("贴牌id不能为空");
        }
        if (getChannelId() == null) {
            throw new ServiceException("通道id不能为空");
        }
        UserGrade userGrade = UserGrade.of(getGrade());
        if (userGrade == null) {
            throw new ServiceException("等级不能为空");
        }
        setUserGrade(userGrade);
        PayType payType = PayType.of(getType());
        if (payType == null) {
            throw new ServiceException("交易类型不能为空");
        }
        setPayType(payType);
        ChannelMerType channelMerType = ChannelMerType.of(getMerType());
        if (channelMerType == null) {
            throw new ServiceException("通道商户类型不能为空");
        }
        setChannelMerType(channelMerType);
        if (getId() == null) {
            validCreate();
        } else {
            validUpdate();
        }
        if (getRate() != null) {
            setRate(getRate().setScale(4, BigDecimal.ROUND_HALF_DOWN));
        }
        if (getMinRate() != null) {
            setMinRate(getMinRate().setScale(4, BigDecimal.ROUND_HALF_DOWN));
        }
        if (getExtraFee() != null) {
            setExtraFee(getExtraFee().setScale(2, BigDecimal.ROUND_HALF_DOWN));
        }
        if (getGradeRate() != null) {
            setGradeRate(getGradeRate().setScale(4, BigDecimal.ROUND_HALF_DOWN));
        }
    }

    private void validCreate() {
        if (getRate() == null) {
            throw new ServiceException("费率不能为空");
        }
        if (getRate().compareTo(BigDecimal.ZERO) <= 0 || getRate().compareTo(BigDecimal.ONE) >= 0) {
            throw new ServiceException("费率必须大于0小于1");
        }
        if (getMinRate() == null) {
            throw new ServiceException("最低费率不能为空");
        }
        if (getMinRate().compareTo(BigDecimal.ZERO) <= 0 || getMinRate().compareTo(BigDecimal.ONE) >= 0) {
            throw new ServiceException("最低费率必须大于0小于1");
        }
        if (getRate().compareTo(getMinRate()) < 0) {
            throw new ServiceException("费率不能小于最低费率");
        }
        if (getExtraFee() == null || getExtraFee().compareTo(BigDecimal.ZERO) < 0) {
            throw new ServiceException("手续费不能为空且不能小于0");
        }
        if (getGradeRate() == null || getGradeRate().compareTo(BigDecimal.ZERO) < 0) {
            throw new ServiceException("推荐人分润费率不能为空且不能小于0");
        }
    }

    private void validUpdate() {
        if (getRate() == null && getMinRate() == null &&
                getExtraFee() == null && getGradeRate() == null) {
            throw new ServiceException("请确定需要修改的内容");
        }
        if (getRate() != null && (getRate().compareTo(BigDecimal.ZERO) <= 0 || getRate().compareTo(BigDecimal.ONE) >= 0)) {
            throw new ServiceException("费率必须大于0小于1");
        }
        if (getMinRate() != null && (getMinRate().compareTo(BigDecimal.ZERO) <= 0 || getMinRate().compareTo(BigDecimal.ONE) >= 0)) {
            throw new ServiceException("最低费率必须大于0小于1");
        }
        if (getRate() != null && getMinRate() != null && getRate().compareTo(getMinRate()) < 0) {
            throw new ServiceException("费率不能小于最低费率");
        }
        if (getExtraFee() != null && getExtraFee().compareTo(BigDecimal.ZERO) < 0) {
            throw new ServiceException("手续费不能为空且不能小于0");
        }
        if (getGradeRate() != null && getGradeRate().compareTo(BigDecimal.ZERO) < 0) {
            throw new ServiceException("推荐人分润费率不能为空且不能小于0");
        }
    }

    public void validQuery() {
        if (getBrandId() == null || getType() == null || getChannelId() == null) {
            throw new ServiceException("缺少必要查询参数");
        }
    }
}
